package com.qlzb.taskmanagement.service.timingTask.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qlzb.taskmanagement.repositoty.TimingRepository;
import com.qlzb.taskmanagement.utils.SnowflakeUtils;
import com.qlzb.taskmanagement.vo.form.TimingTaskFormVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.qlzb.taskmanagement.entity.TimingTask;
import com.qlzb.taskmanagement.pojo.OutputWrapper;
import com.qlzb.taskmanagement.service.timingTask.TimingTaskService;
import com.qlzb.taskmanagement.utils.ConstantConfiguration;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName TimingTaskServiceImpl
 * @Description TODO
 * @Authr ryf
 * @Date 2020/2/4
 * @Version v2.0
 */

@Transactional
@Service
public class TimingTaskServiceImpl  implements TimingTaskService {

    @Autowired
    private TimingRepository timingRepositoty;

    @Override
    public OutputWrapper findById(String id) {
        TimingTask timingTask = timingRepositoty.findByByPrimaryKey(id);
        if (!StringUtils.isEmpty(timingTask)) {
            return OutputWrapper.markSelectSuccess(timingTask);
        }

        return OutputWrapper.markSelectError();
    }

    @Override
    public OutputWrapper add(TimingTaskFormVo vo) {

            TimingTaskFormVo timingTaskFormVo = TimingTaskFormVo.class.cast(vo);
            TimingTask timingTask = new TimingTask();

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = df.format(new Date());
            long id = SnowflakeUtils.nextId();

            timingTask.setId(String.valueOf(id));
            timingTask.setTaskCode(timingTaskFormVo.getTaskCode());
            timingTask.setServerIp(timingTaskFormVo.getServerIp());
            timingTask.setStatus(timingTaskFormVo.getStatus());
            timingTask.setTaskName(timingTaskFormVo.getTaskName());
            timingTask.setTaskExecuteNum(timingTaskFormVo.getTaskExecuteNum());
            timingTask.setLastTime(timingTaskFormVo.getLastTime());
            timingTask.setIntervalNum(timingTaskFormVo.getIntervalNum());
            timingTask.setCreateTime(time);
            if (!StringUtils.isEmpty(timingTaskFormVo.getConfig())){
                timingTask.setConfig(timingTaskFormVo.getConfig());
            }
            timingRepositoty.save(timingTask);

            return OutputWrapper.markInsertSuccess(1);
    }

    @Override
    public OutputWrapper modify(TimingTaskFormVo vo) {

            TimingTaskFormVo timingTaskFormVo = TimingTaskFormVo.class.cast(vo);
            TimingTask timingTask = timingRepositoty.findByByPrimaryKey(vo.getId());

            timingTask.setId(timingTaskFormVo.getId());
            timingTask.setServerIp(timingTaskFormVo.getServerIp());
            timingTask.setTaskCode(timingTaskFormVo.getTaskCode());
            timingTask.setStatus(timingTaskFormVo.getStatus());
            timingTask.setTaskName(timingTaskFormVo.getTaskName());
            timingTask.setTaskExecuteNum(timingTaskFormVo.getTaskExecuteNum());
            timingTask.setLastTime(timingTaskFormVo.getLastTime());
            timingTask.setIntervalNum(timingTaskFormVo.getIntervalNum());
            if (!StringUtils.isEmpty(timingTaskFormVo.getConfig())){
                timingTask.setConfig(timingTaskFormVo.getConfig());
            }

            timingRepositoty.save(timingTask);

            return OutputWrapper.markUpdateSuccess(1);
    }

    @Override
    public OutputWrapper removeById(String id) {

        TimingTask timingTask = timingRepositoty.findByByPrimaryKey(id);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        byte delFlag = 1;

        timingTask.setId(id);
        timingTask.setDelFlag(delFlag);
        timingTask.setUpdateTime(time);

        timingRepositoty.save(timingTask);

        return OutputWrapper.markDeleteSuccess(1);

    }

    @Override
    public OutputWrapper findAllAndPageByQueryVo(String pageNum,String pageSize) {

            int pageNumInt = StringUtils.isEmpty(pageNum) ? ConstantConfiguration.PAGE_NUM : Integer.parseInt(pageNum);
            int pageSizeInt = StringUtils.isEmpty(pageSize) ? ConstantConfiguration.PAGE_SIZE : Integer.parseInt(pageSize);

            PageHelper.startPage(pageNumInt,pageSizeInt);

            List<TimingTask> TimingTasks = timingRepositoty.findAll();
            List<Map<String, String>> list = new ArrayList<>();
            for (TimingTask timingTask : TimingTasks) {
                list.add(JSON.parseObject(JSON.toJSONString(timingTask), new TypeReference<Map<String, String>>() {}));
            }

            PageInfo<Map<String, String>> pageInfo = new PageInfo<>(list);

            return OutputWrapper.markSelectSuccess(pageInfo);
    }
}
